package com.mk.service.platform;

import act.controller.Controller;
import cn.hutool.core.date.DateUtil;
import com.github.d925529.apidoc.ApiHttpMethod;
import com.github.d925529.apidoc.annotation.Api;
import com.github.d925529.apidoc.annotation.ApiMethod;
import com.github.d925529.apidoc.annotation.ApiParam;
import com.github.d925529.apidoc.annotation.ApiReturn;
import com.mk.domain.TMbRevenueFlow;
import com.mk.domain.TOrderDetail;
import com.mk.enums.GlobalValues;
import com.mk.interceptor.ITranslate;
import com.mk.service.BasePaasService;
import com.mk.util.MapUtil;
import com.mk.util.S2;
import org.beetl.sql.core.engine.PageQuery;
import org.osgl.mvc.annotation.GetAction;
import org.osgl.mvc.annotation.PostAction;
import org.osgl.mvc.annotation.PutAction;
import org.osgl.mvc.annotation.With;
import org.osgl.util.C;

import javax.inject.Inject;
import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 说明：会员奖励结算流水后台服务
 * 作者：292303959@qq.com 
 * 日期：2018-10-17 10:6:12 
 * 版本：1.0.0
 */
@Controller("tMbRevenueFlow")
@Api(group = "pl",title = "会员奖励结算流水服务", path = "/tMbRevenueFlow")
@SuppressWarnings("unused")
public class TMbRevenueFlowService extends BasePlatformService {
    @Inject
    private TMbRevenueFlow.Mapper tMbRevenueFlowMapper;
    @Inject
    private BasePaasService basePaasService;



    @With({ILogin.class, ITranslate.class})
    @PostAction("add.json")
    @ApiMethod(title = "增加保存会员奖励结算流水", path = "/add.json", method = ApiHttpMethod.POST)
    @ApiParam(name = "tMbRevenueFlow", title = "会员奖励结算流水", type = TMbRevenueFlow.class)
    @ApiReturn(description = "主键", type = String.class)
    public String add(@NotNull TMbRevenueFlow tMbRevenueFlow) {
        String id = S2.UUID();
        tMbRevenueFlow.setRfid(id);
        tMbRevenueFlowMapper.insertTemplate(tMbRevenueFlow);
        return id;
    }


    @With({ILogin.class, ITranslate.class})
    @PutAction("update.json")
    @ApiMethod(title = "修改保存会员奖励结算流水", path = "/update.json", method = ApiHttpMethod.PUT)
    @ApiParam(name = "tMbRevenueFlow", title = "会员奖励结算流水", type = TMbRevenueFlow.class)
    @ApiReturn(description = "修改了的数量", type = int.class)
    public int update(@NotNull TMbRevenueFlow tMbRevenueFlow) {
        return tMbRevenueFlowMapper.updateTemplateById(tMbRevenueFlow);
    }

    @With(ILogin.class)
    @GetAction("view.json")
    @ApiMethod(title = "查询指定会员奖励结算流水", path = "/view.json", method = ApiHttpMethod.GET)
    @ApiParam(name = "id", title = "会员奖励结算流水的主键", type = String.class)
    @ApiReturn(description = "会员奖励结算流水", type = TMbRevenueFlow.class)
    public TMbRevenueFlow view(@NotNull String id){
        return tMbRevenueFlowMapper.single(id);
    }


    @With({ILogin.class, ITranslate.class})
    @PostAction("buildMflow.json")
    @ApiMethod(title = "生成会员奖励结算流水", path = "/buildMflow.json", method = ApiHttpMethod.POST)
    @ApiParam(name = "tMbRevenueFlow", title = "会员奖励结算流水", type = TMbRevenueFlow.class)
    @ApiReturn(description = "主键", type = String.class)
    public int buildMflow() {
        String nowDate=DateUtil.now();
        List<Map>  odList = tMbRevenueFlowMapper.getSQLManager().select("TMbRevenueFlow.select_mb_revenue",Map.class,C.Map("nowDate",nowDate));
        odList.forEach(map->{
            String mid= MapUtil.getStr(map,"pmid");
            BigDecimal amount= new BigDecimal(null==MapUtil.getStr(map,"amount")?"0":MapUtil.getStr(map,"amount"));
            String rfid=S2.UUID();
            //更新订单
            tMbRevenueFlowMapper.updateOrderDetail(rfid,nowDate);
            //插入结算单
            TMbRevenueFlow tMbRevenue =new TMbRevenueFlow();
            tMbRevenue.setRfid(rfid);
            tMbRevenue.setMid(mid);
            tMbRevenue.setRfdate(nowDate);
            tMbRevenue.setRfamount(amount);
            tMbRevenue.setPaystate(GlobalValues.Z_MB_FLOW_PAY_0.value());
            tMbRevenue.setRftype(GlobalValues.Z_MB_FLOW_TYPE_0.value());
            tMbRevenueFlowMapper.insert(tMbRevenue);
        });
        return 0;
    }

    @With({ILogin.class, ITranslate.class})
    @PostAction("buildPreMflow.json")
    @ApiMethod(title = "生成会长奖励结算流水", path = "/buildPreMflow.json", method = ApiHttpMethod.POST)
    @ApiParam(name = "tMbRevenueFlow", title = "生成会长奖励结算流水", type = TMbRevenueFlow.class)
    @ApiReturn(description = "主键", type = String.class)
    public int buildPreMflow() {
        String nowDate=DateUtil.now();
        List<Map>  odList = tMbRevenueFlowMapper.getSQLManager().select("TMbRevenueFlow.select_pmb_revenue",Map.class, C.Map("nowDate",nowDate));
        odList.forEach(map->{
            String mid= MapUtil.getStr(map,"mid");
            BigDecimal amount= new BigDecimal(null==MapUtil.getStr(map,"amount")?"0":MapUtil.getStr(map,"amount"));
            if(amount.compareTo(BigDecimal.ZERO)>0){
                String rfid=S2.UUID();
                //更新订单
                tMbRevenueFlowMapper.updateOrderDetailByDate(rfid,nowDate);
                //插入结算单
                TMbRevenueFlow tMbRevenue =new TMbRevenueFlow();
                tMbRevenue.setRfid(rfid);
                tMbRevenue.setMid(mid);
                tMbRevenue.setRfdate(nowDate);
                tMbRevenue.setRfamount(amount);
                tMbRevenue.setPaystate(GlobalValues.Z_MB_FLOW_PAY_0.value());
                tMbRevenue.setRftype(GlobalValues.Z_MB_FLOW_TYPE_1.value());
                tMbRevenueFlowMapper.insert(tMbRevenue);
            }
        });
        return 0;
    }

    @With({ILogin.class, ITranslate.class})
    @PostAction("confirmPay.json")
    public int confirmPay(@NotNull String rfid,String rftype) {
        TMbRevenueFlow tMbRevenueFlow = new TMbRevenueFlow();
        tMbRevenueFlow.setRfid(rfid);
        tMbRevenueFlow.setPaydate(DateUtil.now());
        tMbRevenueFlow.setPaystate(GlobalValues.Z_MB_FLOW_PAY_1.value());
        tMbRevenueFlowMapper.updateTemplateById(tMbRevenueFlow);
//        更新订单明细支付标志
        if(GlobalValues.Z_MB_FLOW_TYPE_0.value().equals(rftype)){
            tMbRevenueFlowMapper.updateDetailMpayFlag(rfid);
        }else{
            tMbRevenueFlowMapper.updateDetailPpayFlag(rfid);
        }
        return 0;
    }

    @With(ILogin.class)
    @GetAction("list.json")
    @ApiMethod(title = "查询指定订单明细表", path = "/list.json", method = ApiHttpMethod.GET)
    @ApiParam(name = "orderid", title = "订单ID", type = String.class)
    @ApiReturn(description = "订单明细表", type = TOrderDetail.class)
    public PageQuery<Map> list(String currentPage,
                               String pageSize,
                               String sortName,
                               String sortType
                              ) {
        Map<String, String[]> allParam = context.allParams();
        Map<String, String[]> allParams = new HashMap<>();
        for (Map.Entry<String, String[]> p : allParam.entrySet()) {
            allParams.put(p.getKey(), p.getValue());
        }
        return basePaasService.queryBase("TMbRevenueFlow.select_list_byRfid", currentPage, pageSize, sortName, sortType, allParams);
    }


}
